<?php
namespace App\Admin\Controllers;
use App\Admin\Models\SearchTag;
use App\Admin\Models\SourceMaterial;
use App\Admin\Models\ViewHomeHot;
use Common\ControllerAdBase;

use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class TagsController extends ControllerAdBase
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 推荐搜索标签列表
     */
    public function searchTagsAction()
    {
        $f_search_tag        = new SearchTag();
        $where['conditions'] = "is_show = 0";
        $list                = $f_search_tag->find($where)->toarray();
        $source              = new SourceMaterial();
        foreach ($list as $key => $val) {
            $info                      = $source->find_by_id($val['ids']);
            $list[$key]['icon']        = (int)$val['icon'];
            $list[$key]['source_name'] = $info['name'];
        }
        $this->ok($list);
    }

    public function searchTagsRouterAction()
    {
        if (!isset($this->params['tag_id'])) {
            $this->fail(-1, '标签tag_id不能为空');
        }

        $this->dbMaster->begin();

        switch ($this->params['type']){
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type'] = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case 3:
                $this->params['type'] = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_material_find_one = new SourceMaterial();
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $source_material_find_one = $source_material_find_one->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
        }

        $source_material_find_one->name          = $this->params['name'];
        $source_material_find_one->path          = $this->params['path'];
        $source_material_find_one->type          = $this->params['type'];
        $source_material_find_one->app_path      = $this->params['app_path'];
        $source_material_find_one->url           = $this->params['url'];
        $source_material_find_one->app_data      = $this->params['app_data'];
        $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
        $source_material_find_one->is_need_login = $this->params['is_need_login'];
        $source_material_find_one->title         = $this->params['title'];
        $source_material_find_one->start_color   = $this->params['start_color'];
        $source_material_find_one->end_color     = $this->params['end_color'];
        $source_material_find_one->relationId    = $this->params['relationId'];
        if (!$source_material_find_one->save()) {
            $this->dbMaster->rollback();
            $this->fail(-1, '修改路由失败');
        }

        $info = new SearchTag();
        if ($this->params['tag_id'] > 0) {
            $info = $info->findFirstById($this->params['tag_id']);
        } else {
            $info->created_at = date('Y-m-d H:i:s');
        }

        $info->updated_at = date('Y-m-d H:i:s');
        $info->ids        = $source_material_find_one->id;
        if (!$info->save()) {
            $this->dbMaster->rollback();
            $this->fail(-1, '操作失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }

    /**
     * 推荐搜索标签添加、修改
     */
    public function editSearchTagsAction()
    {
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '缺少参数ids!');
        }
        if (!isset($this->params['icon']) || empty($this->params['icon'])) {
            $this->fail(-1, '缺少参数icon!');
        }
        $f_search_tag = new SearchTag();
        if (isset($this->params['id']) && $this->params['id'] > 0) {
            $f_search_tag = $f_search_tag->find_by_id_obj($this->params['id']);
        }
        $f_search_tag->icon = $this->params['icon'];
        $f_search_tag->ids  = $this->params['ids'];
        if ($f_search_tag->save()) {
            $this->ok();
        }
        $this->fail(-1, '操作失败!');
    }

    /**
     * 推荐搜索标签显示关闭
     */
    public function delSearchTagsAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少参数id!');
        }
        $f_search_tag          = new SearchTag();
        $f_search_tag          = $f_search_tag->find_by_id_obj($this->params['id']);
        $f_search_tag->is_show = $f_search_tag->is_show == 0 ? 1 : 0;
        if ($f_search_tag->save()) {
            $this->ok();
        }
        $this->fail(-1, '操作失败!');
    }

    public function homeHotTagsAction()
    {
        $view_home_hot       = new ViewHomeHot();
        $where['conditions'] = "id in(1,3,4)";
        $list                = $view_home_hot->find($where)->toarray();
        $this->ok($list);
    }

    public function homeHotTagsEditAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少参数id!');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '缺少参数ids!');
        }
        $view_home_hot = new ViewHomeHot();
        $info          = $view_home_hot->findFirstById($this->params['id']);
        if($info->ids){
            if($this->params['ids']) {
                $ids_new = $this->params['ids'];
                $ids_old = explode(',', $info->ids);
                $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                $ids = implode(',', $ids_arr);
            }else{
                $ids = '';
            }

        }else{
            $ids =implode(',', $this->params['ids']);
        }
        $info->ids     = $ids;
        if ($info->save()) {
            $this->ok();
        } else {
            $this->fail(-1, '失败!');
        }
    }

    public function editRouterAction()
    {
        if (!isset($this->params['tag_id']) || empty($this->params['tag_id'])) {
            $this->fail(-1, '标签tag_id不能为空');
        }

        $this->dbMaster->begin();
        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case -2:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_material_find_one = new SourceMaterial();
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $source_material_find_one = $source_material_find_one->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
        }

        $source_material_find_one->name          = $this->params['name'];
        $source_material_find_one->path          = $this->params['path'];
        $source_material_find_one->type          = $this->params['type'];
        $source_material_find_one->app_path      = $this->params['app_path'];
        $source_material_find_one->url           = $this->params['url'];
        $source_material_find_one->app_data      = $this->params['app_data'];
        $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
        $source_material_find_one->is_need_login = $this->params['is_need_login'];
        $source_material_find_one->title         = $this->params['title'];
        $source_material_find_one->start_color   = $this->params['start_color'];
        $source_material_find_one->end_color     = $this->params['end_color'];
        $source_material_find_one->relationId    = $this->params['relationId'] ? '1' : '0';
        if (!$source_material_find_one->save()) {
            $this->dbMaster->rollback();
            $this->fail(-1, '修改路由失败');
        }

        $view_home_hot = new ViewHomeHot();
        $info          = $view_home_hot->findFirstById($this->params['tag_id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }

        $info->ids = $info->ids ? $info->ids . ',' . $source_material_find_one->id : $source_material_find_one->id;
        if (!$info->save()) {
            $this->rollback();
            $this->fail(-1, '操作失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }

    public function delRouterAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '标签id不能为空');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, 'ids不能为空');
        }
        $view_home_hot = new ViewHomeHot();
        $info          = $view_home_hot->findFirstById($this->params['id']);
        if ($info) {
            $olg = explode(',', $info->ids);
            foreach ($olg as $key => $val) {
                if ($val == $this->params['ids']) {
                    unset($olg[$key]);
                }
            }
            $info->ids = implode(',', $olg);
            if ($info->save()) {
                $this->ok('操作成功');
            }
        }
        $this->fail(-1, '操作失败!');
    }

    public function adPositionsAction()
    {
        $f_ad_positions = new AdPositions();
        $list           = $f_ad_positions->find()->toarray();
        $this->ok($list);
    }

    //0一个用户弹一次1一个用户多次
    public function updateAdPositions()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少参数ID');
        }
        if (!isset($this->params['expire_at']) || empty($this->params['expire_at'])) {
            $this->fail(-1, '缺少参数expire_at');
        }
        if (!isset($this->params['num']) || empty($this->params['num'])) {
            $this->fail(-1, '缺少参数num');
        }

        $f_ad_positions  = new AdPositions();
        $info            = $f_ad_positions->findFirstById();
        $info->expire_at = $this->params['expire_at'];
        $info->num       = $this->params['expire_at'];
        if (!$info->save()) {
            $this->fail(-1, '修改失败');
        }
        $this->ok('修改成功!');
    }

    public function editAdRouterAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '标签id不能为空');
        }

        $this->dbMaster->begin();
        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case 3:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_materialDb = new SourceMaterial();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $source_materialDb->name          = $this->params['name'];
            $source_materialDb->path          = $this->params['path'];
            $source_materialDb->type          = $this->params['type'];
            $source_materialDb->app_path      = $this->params['app_path'];
            $source_materialDb->url           = $this->params['url'];
            $source_materialDb->app_data      = $this->params['app_data'];
            $source_materialDb->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_materialDb->is_need_login = $this->params['is_need_login'];
            $source_materialDb->title         = $this->params['title'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->end_color     = $this->params['end_color'];
            $source_materialDb->relationId    = $this->params['relationId'];
            if (!$source_materialDb->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '路由创建失败');
            }
        } else {
            $source_material_find_one = $source_materialDb->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
            $source_material_find_one->name          = $this->params['name'];
            $source_material_find_one->path          = $this->params['path'];
            $source_material_find_one->type          = $this->params['type'];
            $source_material_find_one->app_path      = $this->params['app_path'];
            $source_material_find_one->url           = $this->params['url'];
            $source_material_find_one->app_data      = $this->params['app_data'];
            $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_material_find_one->is_need_login = $this->params['is_need_login'];
            $source_material_find_one->title         = $this->params['title'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->end_color     = $this->params['end_color'];
            $source_material_find_one->relationId    = $this->params['relationId'];
            if (!$source_material_find_one->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '修改路由失败');
            }
        }

        $f_ad_positions = new AdPositions();
        $info           = $f_ad_positions->findFirstById($this->params['id']);
        if (!$info) {
            $this->fail(-1, '不存在');
        }

        $info->ids = $info->ids ? $info->ids . ',' . $source_material_find_one->id : $source_material_find_one->id;
        if (!$info->save()) {
            $this->rollback();
            $this->fail(-1, '操作失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }

    public function delAdRouterAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '标签id不能为空');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, 'ids不能为空');
        }
        $f_ad_positions = new AdPositions();
        $info           = $f_ad_positions->findFirstById($this->params['id']);
        if ($info) {
            $olg = explode(',', $info->ids);
            foreach ($olg as $key => $val) {
                if ($val == $this->params['ids']) {
                    unset($olg[$key]);
                }
            }
            $info->ids = implode(',', $olg);
            if ($info->save()) {
                $this->ok('操作成功');
            }
        }
        $this->fail(-1, '操作失败!');
    }




}
